local jass = require 'jass.common'
local dbg = require 'jass.debug'

local event = {}
ac.event = event

-- [创建触发器]
function event.trigger()
    local handle = jass.CreateTrigger()
    dbg.handle_ref(handle)
    return handle
end 

-- [删除触发器]
function event.remove_trigger(handle)
    if dbg.get_type(handle) == 'trigger' then 
        dbg.handle_unref(handle)
        jass.TriggerClearActions(handle)
        jass.DestroyTrigger(handle)
    end 
end 

event.anyevents = {}
event.callbacks = {}
event.instances = event.trigger()

-- [事件响应]
jass.TriggerAddCondition(event.instances, jass.Condition(function ()
    local eventid = jass.GetTriggerEventId()
    if eventid ~= 0 and event.anyevents[eventid] then 
        local info = event.callbacks[event.anyevents[eventid]]
        if info then 
            for i = 1, #info do 
                info[i]()
            end 
        end
    end 
end))


-- 注册任意回调
function event.register(name, func)
    local info = event.callbacks[name]
    if not info then 
        info = {}
        event.callbacks[name] = info
    end 
    info[#info+1] = func
end

event.anyevents[0x03] = '游戏-时间变化'
event.anyevents[0x60] = '任意玩家-输入聊天信息'
event.anyevents[0x0F] = '任意玩家-离开游戏'
event.anyevents[0x10] = '任意玩家-输入作弊码'
event.anyevents[0x11] = '任意玩家-按下ESC'
event.anyevents[0x12] = '任意单位-被攻击'
event.anyevents[0x18] = '任意单位-被选择'         
event.anyevents[0x19] = '任意单位-被取消选择'
event.anyevents[0x1A] = '任意单位-开始建造'
event.anyevents[0x1B] = '任意单位-取消建造'
event.anyevents[0x1C] = '任意单位-建造完成'
event.anyevents[0x1D] = '任意单位-升级开始'
event.anyevents[0x1E] = '任意单位-取消升级'
event.anyevents[0x1F] = '任意单位-升级完成'
event.anyevents[0X20] = '任意单位-开始训练'
event.anyevents[0X21] = '任意单位-取消训练'
event.anyevents[0X22] = '任意单位-训练完成'
event.anyevents[0X23] = '任意单位-开始研究科技'
event.anyevents[0X24] = '任意单位-取消研究科技'
event.anyevents[0X25] = '任意单位-研究科技完成'
event.anyevents[0X26] = '任意单位-发布无目标命令'
event.anyevents[0X27] = '任意单位-发布点目标命令'
event.anyevents[0X28] = '任意单位-发布指定目标命令'
event.anyevents[0X30] = '任意单位-丢弃物品'
event.anyevents[0X31] = '任意单位-获得物品'
event.anyevents[0X32] = '任意单位-使用物品'
event.anyevents[0X33] = '任意单位-被装载'
-- event.anyevents[271] = '任意单位-购买物品'
event.anyevents[277] = '任意单位-抵押物品'
event.anyevents[272] = '任意单位-准备释放技能'
event.anyevents[273] = '任意单位-开始释放技能'
event.anyevents[274] = '任意单位-发动技能效果'
event.anyevents[275] = '任意单位-释放技能结束'
event.anyevents[276] = '任意单位-停止释放技能'


for i = 1,16 do
    jass.TriggerRegisterPlayerChatEvent(event.instances, jass.Player(i-1), "", false)
    jass.TriggerRegisterPlayerEvent(event.instances, jass.Player(i-1), jass.EVENT_PLAYER_LEAVE)  --离开游戏
    jass.TriggerRegisterPlayerEvent(event.instances, jass.Player(i-1), jass.EVENT_PLAYER_END_CINEMATIC)  --按下ESC
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0x12, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0x18, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0x19, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0x1A, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0x1B, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0x1C, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0x1D, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0x1E, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0x1F, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X20, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X21, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X22, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X23, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X24, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X25, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X26, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X27, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X28, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X30, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X31, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X32, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 0X33, nil) 
    -- jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 271, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 277, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 272, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 273, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 274, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 275, nil) 
    jass.TriggerRegisterPlayerUnitEvent(event.instances, jass.Player(i-1), 276, nil) 
end

jass.TriggerRegisterGameStateEvent(event.instances, jass.GAME_STATE_TIME_OF_DAY, jass.EQUAL, 6.01)
jass.TriggerRegisterGameStateEvent(event.instances, jass.GAME_STATE_TIME_OF_DAY, jass.EQUAL, 18.01)

return event